package com.school.gift.controller.admin;

import com.github.pagehelper.PageInfo;
import com.school.gift.domain.Customer;
import com.school.gift.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import zhibi.common.support.base.Page;
import zhibi.common.support.exception.MessageException;
import zhibi.common.support.exception.TipException;
import zhibi.common.support.mybatis.example.Example;
import zhibi.common.support.mybatis.example.ExampleType;
import zhibi.common.support.mybatis.example.ExampleUtil;
import zhibi.common.util.security.SecurityUtil;

import java.util.Date;

/**
 * Created by 执笔 on 2017/3/21.
 */
@RequestMapping("admin/customer")
@Controller
public class MCustomerController extends AdminController {

    @Autowired
    private CustomerService customerService;

    @RequestMapping("add")
    public String add(){
        return "admin/customer/add";
    }

    @RequestMapping("save")
    public String save(Customer customer){
        Customer temp = new Customer();
        temp.setName(customer.getName());
        temp = customerService.selectOne(temp);
        if(null != temp){
            throw new MessageException("该客服已经存在");
        }
        customer.setPassword(SecurityUtil.MD5(customer.getPassword(),PASSWORD_COUNT));
        customer.setAddtime(new Date());
        customerService.insertSelective(customer);
        return redirect("list");
    }

    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Customer customer = customerService.selectByPK(id);
        if (null == customer) {
            throw new MessageException("该客服不存在");
        }
        model.addAttribute(customer);
        return "admin/customer/detail";
    }

    @RequestMapping("updateCustomer")
    public String updateCustomer(Customer customer){
        customerService.updateByIdSelective(customer);
        return redirect("list");
    }

    @RequestMapping("updateStatus/{id}")
    public String updateStatus(@PathVariable Integer id) {
        Customer customer = customerService.selectByPK(id);
        if (null == customer) {
            throw new MessageException("该客服不存在");
        }
        customer.setStatus((customer.getStatus() + 1) % 2);
        customerService.updateByIdSelective(customer);
        return refresh();
    }

    @RequestMapping("list")
    public String list(Model model, Page page, Customer customer) {
        Example example = ExampleUtil.getInstance()
                .addParam("name", customer.getName(), ExampleType.Operation.LIKE)
                .addParam("username", customer.getUsername(), ExampleType.Operation.LIKE)
                .addOrder("addtime")
                .getExample();
        PageInfo<Customer> pageInfo = customerService.selectByExample(example, page);
        setModelAttribute(model, pageInfo);
        return "admin/customer/list";
    }

    @RequestMapping(value = "login",method = RequestMethod.GET)
    public String login(){
        return "admin/customer/login";
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(String username,String password){
        Customer customer = new Customer();
        customer.setUsername(username);
        customer.setPassword(SecurityUtil.MD5(password,PASSWORD_COUNT));
        customer = customerService.selectOne(customer);
        if(null == customer){
            throw new TipException("用户名或者密码错误","/admin/customer/login");
        }
        if(customer.getStatus() != STATE_1){
            throw new TipException("用户被锁定","/admin/customer/login");
        }
        session.setAttribute(SESSION_USER,customer);
        session.setAttribute(SESSION_TYPE,"customer");
        return redirect("/admin/");
    }
}
